package com.ruoyi.mall.product.domain.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.mall.product.domain.service.IPmsCategoryService;
import com.ruoyi.mall.product.domain.vo.TreeSelect;
import com.ruoyi.mall.product.service.PmsBrandCategoryRelationAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.product.domain.repository.PmsCategoryMapper;
import com.ruoyi.mall.product.domain.entity.PmsCategory;

/**
 * 商品三级分类Service业务层处理
 *
 * @author zhangqishu
 * @date 2022-05-25
 */
@Service("pmsCategoryService")
public class PmsCategoryServiceImpl implements IPmsCategoryService {
    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;

    @Autowired
    private PmsBrandCategoryRelationAppService pmsBrandCategoryRelationAppService;

    /**
     * 查询商品三级分类
     *
     * @param id 商品三级分类主键
     * @return 商品三级分类
     */
    @Override
    public PmsCategory selectPmsCategoryById(Long id) {
        return pmsCategoryMapper.selectPmsCategoryById(id);
    }

    /**
     * 查询商品三级分类列表
     *
     * @param pmsCategory 商品三级分类
     * @return 商品三级分类
     */
    @Override
    public List<PmsCategory> selectPmsCategoryList(PmsCategory pmsCategory) {
        return pmsCategoryMapper.selectPmsCategoryList(pmsCategory);
    }

    /**
     * 新增商品三级分类
     *
     * @param pmsCategory 商品三级分类
     * @return 结果
     */
    @Override
    public int insertPmsCategory(PmsCategory pmsCategory) {
        return pmsCategoryMapper.insertPmsCategory(pmsCategory);
    }

    /**
     * 修改商品三级分类
     *
     * @param pmsCategory 商品三级分类
     * @return 结果
     */
    @Override
    public int updatePmsCategory(PmsCategory pmsCategory) {
        int i = pmsCategoryMapper.updatePmsCategory(pmsCategory);
        if(!StringUtils.isEmpty(pmsCategory.getName())){
            //同步更新
            pmsBrandCategoryRelationAppService.updateByCategoryName(pmsCategory.getId(),pmsCategory.getName());
        }

        return i;
    }

    /**
     * 批量删除商品三级分类
     *
     * @param ids 需要删除的商品三级分类主键
     * @return 结果
     */
    @Override
    public int deletePmsCategoryByIds(Long[] ids) {
        return pmsCategoryMapper.deletePmsCategoryByIds(ids);
    }

    /**
     * 删除商品三级分类信息
     *
     * @param id 商品三级分类主键
     * @return 结果
     */
    @Override
    public int deletePmsCategoryById(Long id) {
        return pmsCategoryMapper.deletePmsCategoryById(id);
    }

    /**
     * 树形结构查询所有分类
     *
     * @return
     */
    @Override
    public List<TreeSelect> listWithTree() {

        //1.查出所有分类
        List<PmsCategory> list = pmsCategoryMapper.selectPmsCategoryList(null);

        // 2.组装成父子结构
        List<PmsCategory> returnList = new ArrayList<>();
        //过滤出所有的id集合
        List<Long> tempList = list.stream().map(PmsCategory::getId).collect(Collectors.toList());

        for (PmsCategory pmsCategory : list) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(pmsCategory.getParentId())) {
                recursionFn(list, pmsCategory);
                returnList.add(pmsCategory);
            }
        }
        if (returnList.isEmpty()) {
            returnList = list;
        }

        return returnList.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public void deleteCategoryByIds(List<Long> ids) {
        //TODO 检查当前删除的菜单，是否被别的地方引用

        //逻辑删除
        pmsCategoryMapper.deleteBatchIds(ids);
    }

    @Override
    public boolean updateBatchById(List<PmsCategory> asList) {

        return false;
    }

    /**
     * 查找三级分类下某个节点的完整路径
     *
     * @param categoryId
     * @return
     */
    @Override
    public Long[] findCategoryPath(Long categoryId) {
        List<Long> list = new ArrayList<>();
        list.add(categoryId);
        PmsCategory pmsCategory = pmsCategoryMapper.selectById(categoryId);
        Long parentId = pmsCategory.getParentId();
        if (parentId != 0) {
            list.add(parentId);
            PmsCategory pmsCategory1 = pmsCategoryMapper.selectById(parentId);
            Long parentId1 = pmsCategory1.getParentId();
            if (parentId1 != 0) {
                list.add(parentId1);
            }
        }
        Collections.sort(list);
        Long[] res = new Long[list.size()];

        return list.toArray(res);
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<PmsCategory> list, PmsCategory t) {
        // 得到子节点列表
        List<PmsCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (PmsCategory tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<PmsCategory> getChildList(List<PmsCategory> list, PmsCategory t) {
        List<PmsCategory> tlist = new ArrayList<>();
        Iterator<PmsCategory> it = list.iterator();
        while (it.hasNext()) {
            PmsCategory n = it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<PmsCategory> list, PmsCategory t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
